<template>
  <div class="channels">
    <div
      class="item"
      v-for="item in showChannels"
      :key="item.channelId"
      :class="{current:item.channelId == currentId}"
      @click="currentChange(item.channelId)"
    >{{item.name}}</div>
    <a href="javascript:;" @click="isCollapse = !isCollapse">{{isCollapse ? "展开" : "折叠"}}</a>
  </div>
</template>

<script>
import { getChannels } from "@/services/services.js";
export default {
  data() {
    return {
      channels: [],
      isCollapse: true,
      currentId: null
    };
  },
  async created() {
    let result = await getChannels();
    this.channels = result;
    this.currentChange(this.channels[0].channelId);
  },
  methods: {
    currentChange(channelId) {
      this.currentId = channelId;
      this.$emit("currentChange", this.currentId);
    }
  },
  computed: {
    showChannels() {
      if (this.isCollapse) {
        return this.channels.slice(0, 15);
      } else {
        return this.channels;
      }
    }
  }
};
</script>

<style lang="less" scoped>
.channels {
  user-select: none;
  width: 1000px;
  // margin: 0 auto;
  padding: 30px 0 15px 0;
  border-bottom: 1px solid #ccc;
  .item {
    font-size: 16px;
    border: 1px solid #ccc;
    display: inline-block;
    padding: 10px 20px;
    border-radius: 5px;
    margin: 0 15px 15px 0;
    cursor: pointer;
    &:hover {
      background-color: #ccc;
    }
    &.current {
      background-color: #000;
      color: #fff;
      border: 1px solid #000;
    }
  }
  a {
    color: #409eff;
    font-size: 17px;
  }
}
</style>